<?php

namespace Liuhelong\LaravelAdmin\Wechat\Http\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountMaterial;
use Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountNew;
use Encore\Admin\Admin;
use Request;
use Log;
use Liuhelong\LaravelAdmin\Wechat\Common;

class WechatOffiaccountMaterialController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '素材';
    protected $description = [
        'index'  => '这里的图文消息用于公众号群发',
        //        'show'   => 'Show',
        //        'edit'   => 'Edit',
        //        'create' => 'Create',
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WechatOffiaccountMaterial());

        $grid->column('media_id',__('laravel_admin_wechat.Media id'))->copyable();
		
		$type= Request::get('type')?:'image';
        if($type=='image'){
		    
			$grid->column('url',__('laravel_admin_wechat.Image'))->image();
			
		}elseif($type=='video'){
		    
            $grid->column('name',__('laravel_admin_wechat.Name'));
			$grid->column('description', __('laravel_admin_wechat.Description'));
			$grid->column('cover_url',__('laravel_admin_wechat.Cover url'))->image();
			
		}elseif($type=='voice'){
		    
			$grid->column('content', __('laravel_admin_wechat.Content'))->display(function($content,$column) use ($type){
            
                return Common::getFile($type,$this->media_id);
            });
			
		}else{
		    
		    $grid->column('content', __('laravel_admin_wechat.Content'))->display(function($content,$column){
            
                return Common::showNews($content);
            });
		}
        $grid->column('update_time',__('laravel_admin_wechat.Updated at'));
        
        $grid->column('tags', __('laravel_admin_wechat.Tag'))->display(function($tags){
			$str = '';
			if(empty($tags))
			    return '';
			    
			foreach($tags as $tag){
				$str .= '<span class="label label-primary"  style="margin:3px;display:inline-block;padding:4px;">'.$tag.'</span><br>';
			}
			return $str;
		})->hide();

		$grid->disableFilter(false);
		$grid->filter(function($filter){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();
        
            // 在这里添加字段过滤器
            $filter->equal('type', __('laravel_admin_wechat.Type'))->radio([
				'image'=>'图片',
				'video'=>'视频',
				'voice'=>'音频',
				'news'=>'图文',
			]);
			
        });
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			$actions->disableEdit();
			
			$actions->add(new \Liuhelong\LaravelAdmin\Wechat\Actions\WechatOffiaccountMaterial\Preview);
			$actions->add(new \Liuhelong\LaravelAdmin\Wechat\Actions\WechatOffiaccountMaterial\BroadCast);
		});
		
		$grid->header(function ($query) {
        
            return '<meta name="referrer" content="never">';
        });
		Admin::style('.table {font-size: 14px;}');
		cache(['material_type'=>$type]);
        return $grid;
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatOffiaccountMaterial());
		
		
		$form->radio('type',  __('laravel_admin_wechat.Type'))->default(cache('material_type','image'))
		->options([
			'image'=>'图片',
			'voice'=>'音频',
			'video'=>'视频',
			//'news'=>'图文',
		])->when('image', function (Form $form) { 

			$form->file('url', '图片')->help('大小不超过 10MB');

		})->when('voice', function (Form $form) { 

			$form->file('url', '音频')->help('大小不超过 5M，长度不超过 60 秒');

		})->when('video', function (Form $form) { 

			$form->file('url', '视频')->help('大小不超过 10MB');
			$form->text('title', __('laravel_admin_wechat.Title'))->help('必填，30字以内');
			$form->textarea('description', __('laravel_admin_wechat.Description'))->help('必填，300字以内');
		});
		
		$form->saved(function (Form $form) {

			return redirect('/admin/wechat/wechat-o-materials?type='.cache('material_type'));

		});

        return $form;
    }
	
	
	public function destroy($id)
    {
        try {
            collect(explode(',', $id))->filter()->each(function ($id) {
                $model = new WechatOffiaccountMaterial();
                $model = $model->findOrFail($id);
                
                $model->delete();
            });

            $response = [
                'status'  => true,
                'message' => trans('admin.delete_succeeded'),
            ];
        } catch (\Exception $exception) {
            $response = [
                'status'  => false,
                'message' => $exception->getMessage() ?: trans('admin.delete_failed'),
            ];
        }

        return response()->json($response);
    }
    
}
